import { Injectable } from '@angular/core'
import { OnInit, OnDestroy } from '@angular/core'
import { IECRouteOfStore } from '@shared/interfaces/store'
import { IBaseInfoColumn } from '@shared/interfaces/baseInfo'
import { EPaymentBrand, ECustomAcl } from '@shared/enums'
import { CustomACLService } from '@shared/services/acl.service'
import { PageService } from '@shared/services/page.service'
@Injectable({
  providedIn: 'root',
})
export class TabECChannelsService implements OnInit, OnDestroy {
  constructor(private pageService: PageService, private customACLService: CustomACLService) {}

  ngOnInit(): void {
    this.initialData()
  }

  ngOnDestroy(): void {
    this.customACLService.removeACLAbility(ECustomAcl.StoreEditable)
  }

  initialData() {
    const isStoreLocked = this.pageService.isStoreLocked
    const isStoreDisplayInTask = this.pageService.isStoreDisplayInTask
    if (!isStoreDisplayInTask && !isStoreLocked) {
      this.customACLService.addACLAbility(ECustomAcl.StoreEditable)
    }
  }

  getChannelColumns(channel: IECRouteOfStore) {
    switch (channel.paymentBrand) {
      case EPaymentBrand.UPI:
        return this.getUPIChannelColumns(channel)
      default:
        return this.getUPIChannelColumns(channel)
    }
  }

  private getUPIChannelColumns(channel: IECRouteOfStore): IBaseInfoColumn[][] {
    return [
      [
        {
          span: 8,
          label: 'Acquiring IIN',
          value: channel.chanMerParams.upiAcquiringIIN,
        },
        {
          span: 8,
          label: 'UPI merchant ID',
          value: channel.chanMerParams.upiMerchantID,
        },
        {
          span: 8,
          label: 'Merchant Abbreviation',
          value: channel.chanMerParams.upiMerchantAbbreviation,
        },
      ],
      [
        {
          span: 12,
          label: 'Transaction Fee',
          value: this.toPercentage(channel.chanMerParams.fee),
        },
        {
          span: 12,
          label: 'OPERATION',
          labelI18n: 'GENERAL.INFO.OPERATION',
          operate: true,
          acl: ['api.put.turtle.store.one.channel', ECustomAcl.StoreEditable],
          aclMode: 'allOf',
        },
      ],
    ]
  }

  private toPercentage(fee: number): string {
    const per = fee * 100
    return `${per.toFixed(4)}%`
  }
}
